package com.co.utils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

public class XMLReader {

	private String filePath = null;

	private static XMLReader xmlReaderFileInstance;
	private HashMap properties;
	private Document xmlContent;

	public static XMLReader getInstance() {
		if (xmlReaderFileInstance == null) {
			xmlReaderFileInstance = new XMLReader();
		}
		return xmlReaderFileInstance;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getFilePath() {
		return this.filePath;
	}

	public String getLookAndFeelValue(Document document) {
		Element root = document.getRootElement();
		return root.getChild("lookandfeel").getAttributeValue("value");
	}

	public String getLookAndFeelValue() {

		String lookAndFeelValue = "";

		SAXBuilder builder = new SAXBuilder();
		File xmlFile = new File(getFilePath());

		try {
			Document document = (Document) builder.build(xmlFile);
			Element rootNode = document.getRootElement();
			Element node = rootNode.getChildren("lookandfeel").get(0);
			System.out.println("Last Name : " + node.getAttributeValue("name"));
			System.out
					.println("Last Name : " + node.getAttributeValue("value"));
			lookAndFeelValue = node.getAttributeValue("value");

			// Element projectNode = rootNode.getChild("project-name");
			// String projectTitle = projectNode.getText();

			// properties.put("lookAndFeel", node.getAttributeValue("value"));
			// properties,put

		} catch (IOException io) {
			System.out.println(io.getMessage());
		} catch (JDOMException jdomex) {
			System.out.println(jdomex.getMessage());
		}

		return lookAndFeelValue;
	}

	public static void main(String[] args) {
		XMLReader xmlReader = XMLReader.getInstance();
		xmlReader.setFilePath("c://file.xml");
		System.out.println("value = " + xmlReader.getLookAndFeelValue());
	}
}